﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sql_Dataprovider;

namespace DACK_N28_QLRCP
{
    public partial class ucQLPhongChieu : UserControl
    {
        public ucQLPhongChieu()
        {
            InitializeComponent();
        }
        public void Load_cbxRapChieu()
        {
            string sql = "select * from Rap";
            Sql_DataProvider Loadcbx = new Sql_DataProvider();
            cbxRapChieu.DataSource = Loadcbx.SQL_ExecuteTable(sql);
            cbxRapChieu.DisplayMember = "TenRap";
            cbxRapChieu.ValueMember = "MaRap";
        }

        public void Load_cbxDK()
        {
            string sql = "select * from KyThuat";
            Sql_DataProvider Loadcbx = new Sql_DataProvider();
            cbxDKKT.DataSource = Loadcbx.SQL_ExecuteTable(sql);
            cbxDKKT.DisplayMember = "TenKyThuat";
            cbxDKKT.ValueMember = "MaKyThuat";
        }

        private void cbxRapChieu_SelectedIndexChanged(object sender, EventArgs e)
        {
                try
                {
                    int id = Convert.ToInt32(cbxRapChieu.SelectedValue.ToString());
                    string sql = "select * from PhongChieu where PhongChieu.MaRap =" + id;
                    Sql_DataProvider LoadTable = new Sql_DataProvider();
                    dgvPCP.DataSource = LoadTable.SQL_ExecuteTable(sql);
                }
                catch { }
           
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            PhongChieu pc = new PhongChieu();
            pc.MaRap = int.Parse(cbxRapChieu.SelectedValue.ToString());
            pc.SoHangGhe = int.Parse(txtHangGhe.Text);
            pc.SoDayGhe = int.Parse(txtDayGhe.Text);
            pc.MaDieuKienKT = int.Parse(cbxDKKT.SelectedValue.ToString());
            pc.TinhTrang = txtTinhTrang.Text;
            pc.MoTa = txtMoTa.Text;
            if (PhongChieu_DAO.ThemPC(pc) == true)
            {
                MessageBox.Show("Thao tác thành công!", "Congratulation", MessageBoxButtons.OK);
                cbxRapChieu_SelectedIndexChanged(null, null);
            }
            else
                MessageBox.Show("Dữ liệu không hợp lệ, vui lòng kiểm tra lại!", "Opps!", MessageBoxButtons.OK);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection index = dgvPCP.SelectedRows;
            foreach (DataGridViewRow A in index)
            {
                int mapc = int.Parse(A.Cells[0].Value.ToString());

                if ((!PhongChieu_DAO.XoaPC(mapc)))
                {
                    MessageBox.Show("Xoá không thành công vui lòng kiểm tra lại kết nối mạng !!!");
                    return;
                }
            }
            MessageBox.Show("Xóa thành công !!!");
            cbxRapChieu_SelectedIndexChanged(null, null);
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            try
            {
                PhongChieu PcNew = new PhongChieu();
                if ((cbxRapChieu.Text != "" & txtHangGhe.Text != "" & txtDayGhe.Text != "" & txtMoTa.Text != "" & txtTinhTrang.Text != "" & cbxDKKT.Text != ""))
                {
                    PcNew.MaPhongChieu = Convert.ToInt32(dgvPCP.CurrentRow.Cells[0].Value);
                    PcNew.MaRap = Convert.ToInt32(cbxRapChieu.SelectedValue.ToString());
                    PcNew.SoHangGhe = int.Parse(txtHangGhe.Text);
                    PcNew.SoDayGhe = int.Parse(txtDayGhe.Text);
                    PcNew.MaDieuKienKT = int.Parse(cbxDKKT.SelectedValue.ToString());
                    PcNew.TinhTrang = txtTinhTrang.Text;
                    PcNew.MoTa = txtMoTa.Text;
                    if ((PhongChieu_DAO.SuaPC(PcNew)))
                    {
                        MessageBox.Show("Cập nhật phòng chiếu thành công !!!");
                        cbxRapChieu_SelectedIndexChanged(null, null);
                    }
                    else
                    {
                        MessageBox.Show("Cập nhật phòng chiếu không thành công !!!");
                    }
                }
            }
            catch { }
        }

        private void dgvPCP_SelectionChanged(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(dgvPCP.CurrentRow.Cells[0].Value);
            PhongChieu pc = new PhongChieu();
            pc = PhongChieu_DAO.Get_PC(id);
            txtHangGhe.Text = pc.SoHangGhe.ToString();
            txtDayGhe.Text = pc.SoDayGhe.ToString();
            txtMoTa.Text = pc.MoTa;
            txtTinhTrang.Text = pc.TinhTrang;
        }

        private void ucQLPhongChieu_Load(object sender, EventArgs e)
        {

        }
    }
}
